<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>核准订单</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <style>
        .layui-form-label{
            width: 120px;
        }
        .layui-input-block{
            margin-left: 160px;
        }
        #modifyForm{
            display: none;
        }
        .productName{
            width: 70%;
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <blockquote class="layui-elem-quote">
            核准订单
            <button type="button" class="layui-btn layui-btn-sm" style="float: right;margin-top: -4px;" onclick="window.history.back();">返回</button>
        </blockquote>
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">订单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="shopOrderNo" autocomplete="off" class="layui-input" placeholder="订单编号" maxlength="30">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="productName" autocomplete="off" class="layui-input" placeholder="商品名称" maxlength="20">
                            </div>
                        </div>
						<div class="layui-inline">
						    <label class="layui-form-label">订单时间</label>
						    <div class="layui-input-inline">
						        <input type="text" class="layui-input" id="selectTime" placeholder=" - ">
						    </div>
						</div>
                        <div class="layui-inline">
                            <button class="layui-btn"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary" >重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <table class="layui-hide" id="selectTable" lay-filter="selectTable"></table>
            </div>
        </div>

        
        <div id="modifyForm">
            <form class="layui-form" action="" lay-filter="modifyForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">商品名称：</label>
                    <div class="layui-form-mid layui-word-aux productName"></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">商品编号：</label>
                    <div class="layui-form-mid layui-word-aux goodNo"></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">商品单位：</label>
                    <div class="layui-form-mid layui-word-aux unit"></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">原价格：</label>
                    <div class="layui-form-mid layui-word-aux goodPrice" style="color: #fbaf7e!important"></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="redtext marginRight">*</span>改单后价格：</label>
                    <div class="layui-input-inline">
                      <input type="text" name="goodPrice" autocomplete="off" placeholder="请输入改单后价格" class="layui-input" maxlength="20">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">原数量：</label>
                    <div class="layui-form-mid layui-word-aux goodCount" style="color: #fbaf7e!important"></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="redtext marginRight">*</span>改单后数量：</label>
                    <div class="layui-input-inline my_count">
                      <input type="text" name="goodCount" autocomplete="off" placeholder="请输入改单后数量" class="layui-input" maxlength="20">
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>

<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'table', 'form', 'laydate'], function() {
        var http = layui.http,
            table = layui.table,
            form = layui.form,
            laydate = layui.laydate;
        var excelList,currPage
        http.checking(function() {

            laydate.render({
                elem: '#selectTime'
                ,theme: '#fbaf7e'
                ,range: "-"
            });

            var selectTable;
            selectTable = http.table({
                elem: '#selectTable',
                url: 'supplier/admin/shop/order/listUpdateOrderPage',
                method: 'get',
                id: 'selectTable',
                done: function(res, curr, count){
                    excelList = res.data;
                    currPage = curr
                },
                cols: [[
                    {title:'序号',type:'numbers',fixed: 'left'}
                    ,{field:'shopOrderNo', title: '订单编号', align : 'center', width: 235,
                        templet: function(d){
                            var html = ''
                            html += '<a class="buttoncolor" href="orderManagementDetail.html?id='+d.shopOrderNo+'">'+d.shopOrderNo+'</a>'
                            return html
                        }
                    }
                    // ,{field:'goodNo', title: '商品编号', align : 'center'}
                    ,{field:'productName', title: '商品名称', align : 'center'}
                    ,{field:'goodPrice', title: '价格', align : 'center'}
                    ,{field:'goodCount', title: '数量', align : 'center'}
                    ,{field:'unitName', title: '单位', align : 'center',
                        templet: function(d){
                            return d.unitName?d.unitName:d.unit;
                        }
                    }
                    ,{field:'canteenName', title: '食堂名称', align : 'center'}
                    ,{field:'createTime', title: '订单时间', align : 'center',width: 170}
                    ,{field:'', title: '操作', align : 'center',fixed: 'right',width: 230,
                        templet: function (d) {
                            var html = '';
                                html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="order" >改单</button>'
                            return html;
                        }
                    }
                ]]
            });

            // //查询
            form.on('submit(searchBth)', function (data) {
                let _data = data.field,
                    param = _data;
				var startTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'';
				var endTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'';
				param.startTime = startTime;
				param.endTime = endTime;
                selectTable.reload({
                    page:{curr:1},
                    where: param
                })
                return false;
            })

            window.onBlurFun = function(e) {
                var unit = $(e).attr('data-unit')
                var count = $(e).attr('data-count')

                var value = e.value
                if(unit == 1){ // 允许输入小数
                    var reg = /^((0|[1-9]\d{0,})((\.[0-9]{1,2})?))$/;
                    if(!reg.test(e.value)) {
                        layer.msg('当前商品数量格式有误')
                        value = count
                    }

                }else{ // 不允许输入小数
                    var regex = /^(0|[1-9][0-9]*)$/;
                    if(!regex.test(value)) {
                        layer.msg('当前商品数量只能为整数')
                        value = parseInt(e.value)
                    }
                }
                $(e).val(value)
            }
            table.on('tool(selectTable)', function(obj){
                var data = obj.data;
                if(obj.event=='order'){
                    // 改单
                    $("#modifyForm .layui-form")[0].reset();
                    $('.productName').html(data.productName)
                    $('.goodNo').html(data.goodNo)
                    $('.unit').html(data.unitName?data.unitName:data.unit)
                    $('.goodPrice').html('￥'+data.goodPrice)
                    $('#modifyForm input[name=goodPrice]').val(data.goodPrice);
                    $('.goodCount').html(data.goodCount)
                    $('#modifyForm input[name=goodCount]').val(data.goodCount);
                    
                    var html = "<input name='goodCount' type='Number' autocomplete='off' placeholder='请输入改单后数量' class='layui-input' value='"+(data.goodCount)+"' onblur=\"onBlurFun(this)\" data-unit='"+data.unitDecimalStatus+"' data-count='"+data.goodCount+"'>"
                    // var html = ''
                    // if(data.unitDecimalStatus === 1){ // 允许输入小数
                    //     html = "<input name='goodCount' autocomplete='off' placeholder='请输入改单后数量' class='layui-input' onkeyup=\"onKeyFloat(this)\" onafterpaste=\"onKeyFloat(this)\" value="+data.goodCount+">"
                    // }else{ // 不允许输入小数
                    //     html = "<input name='goodCount' autocomplete='off' placeholder='请输入改单后数量' class='layui-input' onkeyup=\"onkeyFun(this)\" onafterpaste=\"onkeyFun(this)\" value="+data.goodCount+">"
                    // }
                    $('.my_count').html(html)
                    http.myOpen({
                        title:'改单',
                        content: $('#modifyForm'),
                        area:['600px','500px'],
                        btn:['确定','取消'],
                        btn1:function(index, layero){
                            var goodPrice = $('#modifyForm input[name=goodPrice]').val()
                            var goodCount = $('#modifyForm input[name=goodCount]').val()
                            if(!goodPrice){
                                layer.msg('请输入改单后价格')
                                return
                            }
                            if(!goodCount){
                                layer.msg('请输入改单后数量')
                                return
                            }
                            if(goodPrice == data.goodPrice && goodCount == data.goodCount){
                                layer.msg('价格和数量与原价格和数量相同')
                                return
                            }
                            http.post('supplier/admin/shop/order/updateOrder',{
                                shopOrderNo: data.shopOrderNo,
                                goodId: data.goodNo,
                                goodPrice: goodPrice,
                                goodCount: goodCount
                            },function(res){
                                if(res.code == 200){
                                    layer.msg('操作成功')
                                    layer.close(index)
                                    table.reload('selectTable');
                                }else{
                                    flag = false
                                    layer.msg(res.msg)
                                }
                            })
                        }
                    })
                }
            })
        })

    })

    // 正整数
    function onkeyFun(e){
        if(e.value.length==1){
            e.value = e.value.replace(/[^1-9]/g,'')
        }else{
            e.value = e.value.replace(/\D/g,'')
        }
    }
    // 数字，小数
    function onKeyFloat(e) {
        if(isNaN(e.value))
            document.execCommand('undo')
    }
</script>
</html>